tool
extends EditorPlugin
var b
var type = CONTAINER_TOOLBAR


static func get_children_by_type(node:Node, type)->Array:
	var res:=[]
	for n in node.get_children():
		if n is type:
			res.push_back(n)
	return res

func _enter_tree():

	b = Button.new()
	b.text = "Hello"
	
	var fs_dock: = get_editor_interface().get_file_system_dock()
	var popup_menus =  get_children_by_type(fs_dock, PopupMenu)
	popup_menus[-1].connect("about_to_show", self, "on_context_menu_show",[popup_menus[-1]])
	popup_menus[-1].connect("id_pressed", self, "on_context_menu_id_pressed",[popup_menus[-1]])


func on_context_menu_show(menu:PopupMenu):
	var cur_path:String = get_editor_interface().get_current_path()
	if not cur_path: return
	
	var ext = cur_path.get_extension()
	if ext == "csv":
		menu.add_separator()
		menu.add_item("转换CSV为UTF8")


func on_context_menu_id_pressed(id:int, menu:PopupMenu):
	var cur_path:String = get_editor_interface().get_current_path()
	#获取csv文件的绝对路径（仅编辑器状态下可用）
	var path = ProjectSettings.globalize_path(cur_path)
	print(path)
	var exe_path = "E:/Godot/addons/CsvUtf8"+"/"+"csvutf8.exe"
	OS.execute(exe_path, [path,path])




func _exit_tree():
	pass
